package cn.pomelo.album.advice;

import cn.pomelo.album.commom.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;

/**
 * 全局统一处理器方法拦截
 */
@ControllerAdvice
public class GlobalHandlerAdvice {

    @ExceptionHandler(MultipartException.class)
    @ResponseBody
    public AjaxResult allExceptionHandler(MaxUploadSizeExceededException e) {
        if (e instanceof MaxUploadSizeExceededException) {
            return AjaxResult.failure("文件大小不能超过2MB");
        }
        return AjaxResult.failure(e.getMessage());
    }
}
